﻿using Microsoft.AspNetCore.Identity;
using Snowflake.Core;
using System.Data.Common;
using System.Linq;
using Vending_machine_system.VendingMachineClass;

namespace Vending_machine_system.LogicalLayer
{
    public class UserLayer
    {
        private readonly VendingDBcontent db;
        public UserLayer(VendingDBcontent db)
        {
            this.db = db;
        }
        //注册用户
        public int AddUser(UserInfoClass user)
        {
            var worker = new IdWorker(1, 1);
            user.UserId = worker.NextId();
            var list=db.userInfos.Where(x => x.UserName == user.UserName&&x.Userphone==user.Userphone).ToList();
            if (list.Count > 0)
                return 0;
            else
            db.userInfos.Add(user);
            return db.SaveChanges();
        }

        public UserInfoClass GetLogin(long Userphone,string UserPassword)
        {
            var list = db.userInfos.FirstOrDefault(t=>t.Userphone.Equals(Userphone)&&t.UserPassword.Equals(UserPassword));
            return list;
           
        }
        /// <summary>
        /// 根据用户状态，用户手机号，所属职责，用户姓名等查询分页显示
        /// </summary>
        /// <param name="IsDeleted"></param>
        /// <param name="Userphone"></param>
        /// <param name="UserdutiesId"></param>
        /// <param name="Pageindex"></param>
        /// <param name="Pagesize"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public List<UserInfoClass> GetUsers(out int Tolcount, int Pageindex, int Pagesize,bool IsDeleted,long Userphone=0,long UserdutiesId=0,string UserName="")
        {
            var list = db.userInfos.AsQueryable();
            if (Userphone != 0)
            {
                list = list.Where(t => t.Userphone.Equals(Userphone));
            }
            if (UserdutiesId != 0)
            {
                list = list.Where(t => t.UserdutiesId.Equals(UserdutiesId));
            }
            if (!string.IsNullOrEmpty(UserName))
            {
                list = list.Where(t => t.UserName.Contains(UserName));
            }
            if (IsDeleted==true||IsDeleted==false)
            {
                list = list.Where(t => t.IsDeleted.Equals(IsDeleted));
            }
            Tolcount = list.Count();
            var totalPages = (int)Math.Ceiling((double)Tolcount / Pagesize);
            var items = list.Skip((Pageindex - 1) * Pagesize).Take(Pagesize).ToList();
            return items;
        }
       //查询反填数据
        public UserInfoClass GetUpdate(long UserId)
        {
            var list = db.userInfos.FirstOrDefault(t=>t.UserId.Equals(UserId));
            return list;
        }
        //用户信息删除
        public int Deletion(long UserId)
        {
            var list = db.userInfos.FirstOrDefault(t => t.UserId.Equals(UserId));
            if (list != null)
            db.userInfos.Remove(list);
            return db.SaveChanges();
           
        }
        //用户状态修改
        public int Pseudodeletion(long UserId,bool IsDeleted)
        {
            var list = db.userInfos.FirstOrDefault(t => t.UserId.Equals(UserId));
            list.IsDeleted = IsDeleted;
            return  db.SaveChanges();   
        }
        //用户信息修改
        public int UpdateUser(UserInfoClass userInfo)
        {
            db.userInfos.Update(userInfo);
            return db.SaveChanges();
        }

    }
}
